import { exec } from 'child_process';
import * as fs from 'fs'
import * as path from 'path'
import BuildCondig from './publish.config.js'
// import _package from '../package.json';


var str = fs.readFileSync('./package.json', 'utf-8');
var packageObj = JSON.parse(str);

// console.log(packageObj);

var _date = new Date();
var zeroize = num => num < 10 ? ('0' + num) : num;
var Version = packageObj.version + '.' + zeroize(_date.getMonth() + 1) + zeroize(_date.getDate());
var regex = /(?<=version:\s\').*?(?=\'\,)/;



var replaceVersion = function (JSpath, version) {
    console.log('\x1b[36m%s\x1b[0m', '修改配置文件：' + JSpath);
    return new Promise((resolve, reject) => {
        try {
            // var filePath = path.join(__dirname, JSpath);
            var DevConfig = fs.readFileSync(JSpath, 'utf-8');
            var str = DevConfig.replace(regex, function (i) {
                return version;
            })
            console.log("\x1b[32m%s\x1b[0m", '修改配置完成：' + JSpath + ' 👏');
            resolve();
            fs.writeFileSync(JSpath, str);
        } catch (error) {
            console.error("\x1b[31m%s\x1b[0m", '修改配置文件报错');
            console.log(error);
        }
    });
}

var buildEvent = function () {
    console.log('\x1b[36m%s\x1b[0m', 'vue cli build 打包中...');
    return new Promise((resolve, reject) => {
        exec('npm run build', function (error, stdout, strdrr) {
            if (error) {
                console.log("\x1b[31m%s\x1b[0m", 'vue cli build 打包失败');
                console.error(error);
                reject();
            } else if (strdrr.length > 0) {
                // console.log(strdrr.toString());
                console.log("\x1b[32m%s\x1b[0m", 'vue cli build 打包完成 👏');
                resolve();
            } else {
                // console.log(stdout);
                console.log("\x1b[32m%s\x1b[0m", 'vue cli build 打包完成 👏');
                resolve();
            }
        })
    })
}

var DockerEvent = function () {
    console.log('\x1b[36m%s\x1b[0m', 'docker 打包中...');
    return new Promise((resolve, reject) => {
        exec(`docker build -f ./publish/Dockerfile -t ${BuildCondig.buildStr}:${Version} .`, function (error, stdout, strdrr) {
            if (error) {
                console.log("\x1b[31m%s\x1b[0m", 'docker 打包失败');
                console.error(error);
                reject();
            } else if (strdrr.length > 0) {
                console.log("\x1b[32m%s\x1b[0m", 'docker 打包完成 👏');
                resolve();
            } else {
                console.log("\x1b[32m%s\x1b[0m", 'docker 打包完成 👏');
                console.log(stdout);
                resolve();
            }
        })
    })
}

var PushEvent = function () {
    console.log('\x1b[36m%s\x1b[0m', 'push 推送中...');
    return new Promise((resolve, reject) => {
        exec(`docker push ${BuildCondig.buildStr}:${Version}`, function (error, stdout, strdrr) {
            if (error) {
                cconsole.log("\x1b[31m%s\x1b[0m", 'push 推送失败');
                reject();
            } else if (strdrr.length > 0) {
                console.log("\x1b[32m%s\x1b[0m", 'push 推送完成 👏');
                resolve();
            } else {
                console.log("\x1b[32m%s\x1b[0m", 'push 推送完成 👏');
                resolve();
            }
        })
    })
}


var RunEvent = async function () {
    console.log('\x1b[36m%s\x1b[0m', '新版本打包进行中 👀');
    console.log('当前版本号：', "\x1b[32m" + Version + "\x1b[0m");
    try {
        await replaceVersion('./public/config.js', Version);
        await replaceVersion('./src/@base/dev.config.js', Version);
        await buildEvent();
        await DockerEvent();
        await PushEvent();
        console.log("\x1b[34m%s\x1b[0m", '全部完成 😎');
    } catch (error) {
        console.error(error);
    }
}


RunEvent();